﻿using System.Data;

namespace Efficient.DataAccess
{

    /// <summary>
    /// 提供对操作数据库的方法
    /// </summary>
    public interface IDbService : IExtendDbService
    {
        bool PagingNeedOrder { get; }
        /// <summary>
        /// 
        /// </summary>
        char CloseQuote { get; }
        /// <summary>
        /// 
        /// </summary>
        char OpenQuote { get; }
        /// <summary>
        /// 
        /// </summary>
        bool SupportLimit { get; }
        /// <summary>
        /// 
        /// </summary>
        bool SupportLimitOffset { get; }
        /// <summary>
        /// 数据参数前缀
        /// </summary>
        string ParamNamedPrefix { get; }
        /// <summary>
        /// 创建DbCommand对象
        /// </summary>
        /// <returns>DbCommand</returns>
        IDbCommand CreateCommand();

        /// <summary>
        /// 创建数据库连接
        /// </summary>
        /// <returns>DbConnection</returns>
        IDbConnection CreateConnection();
        /// <summary>
        /// 创建数据库连接
        /// </summary>
        /// <param name="connectionString">数据库连接串</param>
        /// <returns>DbConnection</returns>
        IDbConnection CreateConnection(string connectionString);
        /// <summary>
        /// 创建DbParameter
        /// </summary>
        /// <returns>DbParameter</returns>
        IDbDataParameter CreateParameter();
        /// <summary>
        /// 创建DbParameter
        /// </summary>
        /// <returns></returns>
        IDbDataParameter CreateParameter(string paramName, ParameterDirection direction, DbType dbType);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="paramName"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        IDbDataParameter CreateParameter(string paramName, ParameterDirection direction, object val);
        /// <summary>
        /// 创建DbParameter
        /// </summary>
        /// <returns></returns>
        IDbDataParameter CreateParameter(string paramName, ParameterDirection direction, DbType dbType, int size, object val);

        /// <summary>
        /// 创建DbParameter
        /// </summary>
        /// <returns></returns>
        IDbDataParameter CreateParameter(string paramName, ParameterDirection direction, DbType dbType, object val);
        
        /// <summary>
        /// 创建DataAdapter
        /// </summary>
        /// <returns></returns>
        IDbDataAdapter CreateDataAdapter();
        /// <summary>
        /// 把查询的参数添加到DbCommand
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="commandParameters"></param>
        void AttachParameter(IDbCommand cmd, params IDbDataParameter[] commandParameters);
        /// <summary>
        /// 执行语句返回一个数据集
        /// </summary>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句</param>
        /// <param name="commandParameters">参数，如无参，此为null</param>
        /// <returns>检索到的数据集</returns>
        DataSet ExecuteDataSet(CommandType cmdType, string cmdText,
                               params IDbDataParameter[] commandParameters);

        /// <summary>
        /// 执行语句返回一个数据集
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="cmdType">数据类型</param>
        /// <param name="cmdText">数据命令</param>
        /// <param name="commandParameters">参数，如无参，此为null</param>
        /// <returns>检索到的数据集</returns>
        DataSet ExecuteDataSet(IDbConnection connection, CommandType cmdType, string cmdText,
                               params IDbDataParameter[] commandParameters);
        /// <summary>
        /// 执行语句返回一个数据集
        /// </summary>
        /// <param name="tran">数据库事务</param>
        /// <param name="cmdType">数据类型</param>
        /// <param name="cmdText">数据命令</param>
        /// <param name="commandParameters">参数，如无参，此为null</param>
        /// <returns>检索到的数据集</returns>
        DataSet ExecuteDataSet(IDbTransaction tran, CommandType cmdType, string cmdText,
                               params IDbDataParameter[] commandParameters);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        DataSet ExecuteDataSet(IDbCommand cmd);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="cmd"></param>
        /// <returns></returns>
        DataSet ExecuteDataSet(IDbConnection conn, IDbCommand cmd);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tran"></param>
        /// <param name="cmd"></param>
        /// <returns></returns>
        DataSet ExecuteDataSet(IDbTransaction tran, IDbCommand cmd);
       
        /// <summary>
        /// 执行语句检索一个DataTable
        /// </summary>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句</param>
        /// <param name="commandParameters">参数，如无参，此为null</param>
        /// <returns>检索到的数据表</returns>
        DataTable ExecuteDataTable(CommandType cmdType, string cmdText,
                                   params IDbDataParameter[] commandParameters);

        /// <summary>
        /// 执行语句检索一个DataTable
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句</param>
        /// <param name="commandParameters">参数，如无参，此为null</param>
        /// <returns>检索到的数据表</returns>
        DataTable ExecuteDataTable(IDbConnection connection, CommandType cmdType, string cmdText,
                                   params IDbDataParameter[] commandParameters);
        /// <summary>
        /// 执行语句检索一个DataTable
        /// </summary>
        /// <param name="tran">数据库事务</param>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句</param>
        /// <param name="commandParameters">参数，如无参，此为null</param>
        /// <returns>检索到的数据表</returns>
        DataTable ExecuteDataTable(IDbTransaction tran, CommandType cmdType, string cmdText,
                                   params IDbDataParameter[] commandParameters);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        DataTable ExecuteDataTable(IDbCommand cmd);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="cmd"></param>
        /// <returns></returns>
        DataTable ExecuteDataTable(IDbConnection conn, IDbCommand cmd);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tran"></param>
        /// <param name="cmd"></param>
        /// <returns></returns>
        DataTable ExecuteDataTable(IDbTransaction tran, IDbCommand cmd);
        
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句或存储过程名称</param>
        /// <param name="commandParameters">参数</param>
        /// <returns>影响的行数</returns>
        int ExecuteNonQuery(CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters);
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句或存储过程名称</param>
        /// <param name="commandParameters">参数</param>
        /// <returns>影响的行数</returns>
        int ExecuteNonQuery(IDbConnection connection, CommandType cmdType, string cmdText,
                            params IDbDataParameter[] commandParameters);
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="tran">数据库事务</param>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句或存储过程名称</param>
        /// <param name="commandParameters">参数</param>
        /// <returns>影响的行数</returns>
        int ExecuteNonQuery(IDbTransaction tran, CommandType cmdType, string cmdText,
                            params IDbDataParameter[] commandParameters);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        int ExecuteNonQuery(IDbCommand cmd);
        /// <summary>
        /// 采用现有的数据库连接,执行数据库Command方法不负责开启数据库连接
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="conn"></param>
        /// <exception cref="System.InvalidOperationException">当数据库连接没有开启时可能引发此异常</exception>
        /// <returns></returns>
        int ExecuteNonQuery(IDbConnection conn, IDbCommand cmd);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        int ExecuteNonQuery(IDbTransaction tran, IDbCommand cmd);
        /// <summary>
        /// ExecuteReader
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句或存储过程名称</param>
        /// <param name="commandParameters">参数</param>
        /// <returns>DbDataReader</returns>
        IDataReader ExecuteReader(IDbConnection connection, CommandType cmdType,
                                   string cmdText, params IDbDataParameter[] commandParameters);
        /// <summary>
        /// ExecuteReader
        /// </summary>
        /// <param name="tran">数据库事务</param>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句或存储过程名称</param>
        /// <param name="commandParameters">参数</param>
        /// <returns>DbDataReader</returns>
        IDataReader ExecuteReader(IDbTransaction tran, CommandType cmdType,
                                   string cmdText, params IDbDataParameter[] commandParameters);
        /// <summary>
        /// ExecuteReader
        /// </summary>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句或存储过程名称</param>
        /// <param name="commandParameters">参数</param>
        /// <returns>DbDataReader</returns>
        IDataReader ExecuteReader(CommandType cmdType, string cmdText,
                                   params IDbDataParameter[] commandParameters);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="cmd"></param>
        /// <returns></returns>
        IDataReader ExecuteReader(IDbConnection conn, IDbCommand cmd);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="cmd"></param>
        /// <returns></returns>
        IDataReader ExecuteReader(IDbTransaction tran, IDbCommand cmd);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="cmd"></param>
        /// <returns></returns>
        IDataReader ExecuteReader(IDbCommand cmd);
        /// <summary>
        /// ExecuteScalar
        /// </summary>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句或存储过程名称</param>
        /// <param name="commandParameters">参数</param>
        /// <returns>DbDataReader</returns>
        object ExecuteScalar(CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters);
        /// <summary>
        /// ExecuteScalar
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句或存储过程名称</param>
        /// <param name="commandParameters">参数</param>
        /// <returns>DbDataReader</returns>
        object ExecuteScalar(IDbConnection connection, CommandType cmdType, string cmdText,
                             params IDbDataParameter[] commandParameters);
        /// <summary>
        /// ExecuteScalar
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句或存储过程名称</param>
        /// <param name="commandParameters">参数</param>
        /// <returns>DbDataReader</returns>
        object ExecuteScalar(IDbTransaction tran, CommandType cmdType, string cmdText,
                             params IDbDataParameter[] commandParameters);
        /// <summary>
        /// ExecuteScalar
        /// </summary>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句或存储过程名称</param>
        /// <param name="commandParameters">参数</param>
        /// <returns>DbDataReader</returns>
        object ExecuteScalar(IDbCommand cmd);
        /// <summary>
        /// ExecuteScalar
        /// </summary>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句或存储过程名称</param>
        /// <param name="commandParameters">参数</param>
        /// <returns>DbDataReader</returns>
        object ExecuteScalar(IDbConnection conn, IDbCommand cmd);
        /// <summary>
        /// ExecuteScalar
        /// </summary>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">命令语句或存储过程名称</param>
        /// <param name="commandParameters">参数</param>
        /// <returns>DbDataReader</returns>
        object ExecuteScalar(IDbTransaction tran, IDbCommand cmd);
        /// <summary>
        /// 创建一个事务
        /// </summary>
        /// <returns>事务</returns>
        IDbTransaction BeginTransaction();
        /// <summary>
        /// 创建一个事务
        /// </summary>
        /// <param name="level">数据隔离级别</param>
        /// <returns>事务</returns>
        IDbTransaction BeginTransaction(IsolationLevel level);
        /// <summary>
        /// 回滚一个事务
        /// </summary>
        /// <param name="tran">事务对象</param>
        void Rollback(IDbTransaction tran);
        /// <summary>
        /// 提交一个事务
        /// </summary>
        /// <param name="tran">事务对象</param>
        void Commit(IDbTransaction tran);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rawQuery"></param>
        /// <returns></returns>
        string BuildComputeCountQuery(string rawQuery);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rawQuery"></param>
        /// <returns></returns>
        string MaxResultQuery(string rawQuery, uint maxRow);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="rawQuery"></param>
        /// <returns></returns>
        string BuildPagingResultQueryNeedOrder(string rawQuery,string orderString, string startParam, string endParam);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rawQuery"></param>
        /// <returns></returns>
        string BuildFirstResultQueryNeedOrder(string rawQuery, string orderString, string startParam);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rawQuery"></param>
        /// <returns></returns>
        string BuildPagingResultQuery(string rawQuery, string startParam, string endParam);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rawQuery"></param>
        /// <returns></returns>
        string BuildFirstResultQuery(string rawQuery, string startParam);

        
    }
}